package cn.cvs.service.impl;

import cn.cvs.dao.SysRoleMapper;
import cn.cvs.pojo.SysRole;
import cn.cvs.service.SysRoleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    RedisTemplate redisTemplate;

    public List<SysRole> queryAll() {
        Object queryAllRoles = redisTemplate.opsForValue().get("queryAllRoles");
        //查看Redis中是否有这个数据
        if(queryAllRoles!=null){
            return ( List<SysRole>)queryAllRoles;
        }
        //没有的话,就查询出来后放入到缓存区
        List<SysRole> sysRoles = sysRoleMapper.queryAll();
        if(sysRoles!=null){
            //添加入缓存
            redisTemplate.opsForValue().set("queryAllRoles", sysRoles);
        }
        return sysRoles;
    }

    public SysRole queryById(Integer id) {
        return sysRoleMapper.queryById(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int modifyRole(SysRole sysRole) {
        return sysRoleMapper.modifyRole(sysRole);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int insertRole(SysRole sysRole) {
        return sysRoleMapper.saveRole(sysRole);
    }

    @Transactional(rollbackFor = {Exception.class})
    public int deleteRole(Integer id) {
        return sysRoleMapper.deleteRole(id);
    }

    public int queryByRoleIdAndUserCount(Integer id) {
        return sysRoleMapper.queryByRoleIdAndUserCount(id);
    }

    public int queryByCode(String code) {
        return sysRoleMapper.queryByCode(code);
    }
}
